package com.sctvcloud.dongqu.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.LiveAdvanceInfo;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class ChannelDiaFragment extends BaseDialogFragment {

    private ArrayList<LiveAdvanceInfo> liveAdvanceInfos;
    private LayoutInflater inflater;
    private RadioGroup group;
    private OnSelectedListener listener;
    private String currentName;

    public interface OnSelectedListener {
        void onSelected(LiveAdvanceInfo info);
    }

    public void setOnSelectedListener(OnSelectedListener listener) {
        this.listener = listener;
    }

    @Override
    protected int getContentRes() {
        return R.layout.dia_channel_fragment;
    }

    public void setDatas(ArrayList<LiveAdvanceInfo> datas) {
        this.liveAdvanceInfos = datas;
    }

    public void setSelectedData(String selectedData) {
        this.currentName = selectedData;
    }

    @Override
    protected void initViews(final Dialog dialog, Bundle savedInstanceState) {
        if (!ListUtils.isListValued(liveAdvanceInfos)) {
            this.dismissAllowingStateLoss();
        }
        group = (RadioGroup) dialog.findViewById(R.id.dia_channel_choice_rg);
        inflater = LayoutInflater.from(getActivity());
        for (final LiveAdvanceInfo info : liveAdvanceInfos) {
            View v = inflater.inflate(R.layout.item_dia_choice, null);
            RadioButton button = (RadioButton) v.findViewById(R.id.dia_chocie_rb);
            if (info.getName().equals(currentName)) {
                button.setChecked(true);
            } else {
                button.setChecked(false);
            }
            button.setText(info.getName());
            button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (listener != null) {
                            listener.onSelected(info);
                        }
                    }
                }
            });
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            });
            group.addView(v);
        }
    }
}
